package com.example.juc.until;

import java.util.concurrent.locks.ReentrantLock;

public class Lgoupiao implements Runnable{
    private static int num=30;
    private static  ReentrantLock lock =new ReentrantLock();
    @Override
    public void run() {
            while (true) {
                if(num==0)
                    break;
                try {
                    lock.lock();
                    Thread.sleep(50);
                    goupiao();
                } catch (Exception e) {
                    e.getMessage();
                } finally {
                    lock.unlock();
                }
            }
    }

    public void goupiao(){
        if (num>0){
            System.out.println(Thread.currentThread()
                    .getName()+":购买一张，还剩"+num--);
        }
    }

    public static void main(String[] args) {
        Thread t1 =new Thread (new Lgoupiao(),"aa");
        Thread t2 =new Thread (new Lgoupiao(),"bb");
        Thread t3 =new Thread (new Lgoupiao(),"cc");
        t1.start();
        t2.start();
        t3.start();
    }
}
